package org.dromara.mall.dubbo;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.core.utils.BeanUtils;
import org.dromara.mall.domain.TerMallCart;
import org.dromara.mall.domain.TerMallOrder;
import org.dromara.mall.domain.enums.MallStatusContent;
import org.dromara.mall.mapper.TerMallCartMapper;
import org.dromara.mall.mapper.TerMallOrderMapper;
import org.dromara.tearoom.api.RemoteMallOrderService;
import org.dromara.tearoom.api.domain.bo.RemoteHtlOrderBo;
import org.dromara.tearoom.api.domain.vo.RemoteMallOrderVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@RequiredArgsConstructor
@Service
@DubboService
public class RemoteMallOrderImpl implements RemoteMallOrderService {
    private final TerMallOrderMapper baseMapper;
    private final TerMallCartMapper cartMapper;
    public RemoteMallOrderVo queryOrderById(String orderId) {
        return BeanUtils.toBean(baseMapper.selectVoById(orderId),RemoteMallOrderVo.class);
    }

    @Override
    public int updateById(RemoteHtlOrderBo bo) {
        TerMallOrder order=BeanUtils.toBean(bo,TerMallOrder.class);
        order.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(order);
    }

    @Override
    public int deleteByUserId(Long userId){
        LambdaQueryWrapper<TerMallCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(TerMallCart::getUserId,userId);
        lqw.eq(TerMallCart::getSelected,1);
      return   cartMapper.delete(lqw);
    }

    @Override
    public int taskDelete(LocalDateTime date){
        LambdaQueryWrapper<TerMallOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(TerMallOrder::getOrderStatus,MallStatusContent.UN_PAY);
        lqw.le(TerMallOrder::getCreateTime,date.minusHours(1));
        return baseMapper.delete(lqw);
    }
}
